//
//  DiscountCouponViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "DiscountCouponViewController.h"
#import "UnuseCouponViewController.h"
#import "UseRecordViewController.h"
#import "ExpiredViewController.h"

@interface DiscountCouponViewController ()<XXPageTabViewDelegate>
/**
 未使用
 */
@property (nonatomic , strong) UnuseCouponViewController *unuseVC;
/**
 使用记录
 */
@property (nonatomic , strong) UseRecordViewController *useRecordVC;
/**
 已过期
 */
@property (nonatomic , strong) ExpiredViewController *expiredVC;
/**
 pageTabView
 */
@property (nonatomic , strong) XXPageTabView *pageTabView;
/**
 titleArr
 */
@property (nonatomic , strong) NSArray *titleArr;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;


@end

@implementation DiscountCouponViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的豚券";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self addChildViewController:self.unuseVC];
    [self addChildViewController:self.useRecordVC];
    [self addChildViewController:self.expiredVC];
    [self.view addSubview:self.pageTabView];
}

- (void) initLayout {
    [self.pageTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    __block NSString *unuseStr = @"未使用";
    __block NSString *usedStr = @"使用记录";
    __block NSString *expiredStr = @"已过期";
   @weakify(self);
    [self.unuseVC setDiscountCouponsCountBlock:^(int count) {
        @strongify(self);
        unuseStr = [NSString stringWithFormat:@"未使用(%d)",count];
        self.titleArr = @[unuseStr,usedStr,expiredStr];
        [self.pageTabView reloadChildControllers:self.childViewControllers childTitles:self.titleArr];
    }];
    [self.useRecordVC setDiscountCouponsCountBlock:^(int count) {
        @strongify(self);
        usedStr = [NSString stringWithFormat:@"使用记录(%d)",count];
        self.titleArr = @[unuseStr,usedStr,expiredStr];
        [self.pageTabView reloadChildControllers:self.childViewControllers childTitles:self.titleArr];

    }];
    [self.expiredVC setDiscountCouponsCountBlock:^(int count) {
        @strongify(self);
        expiredStr = [NSString stringWithFormat:@"已过期(%d)",count];
        self.titleArr = @[unuseStr,usedStr,expiredStr];
        [self.pageTabView reloadChildControllers:self.childViewControllers childTitles:self.titleArr];
    }];
}

#pragma mark - delegate
- (void)pageTabViewDidEndChange {
    NSLog(@"#####%d", (int)self.pageTabView.selectedTabIndex);

}
#pragma mark - target action
//导航右侧更多按钮
//- (void)moreTapped {
//
//}
- (void)scrollToLast:(id)sender {
    [self.pageTabView setSelectedTabIndexWithAnimation:self.pageTabView.selectedTabIndex-1];
}

- (void)scrollToNext:(id)sender {
    [self.pageTabView setSelectedTabIndexWithAnimation:self.pageTabView.selectedTabIndex+1];
}


#pragma mark - private method
- (void)setUpNav {
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"discountcoupon_more"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(moreTapped)];
}
#pragma mark - getter and setter
- (UnuseCouponViewController *)unuseVC {
    if (!_unuseVC) {
        _unuseVC = [[UnuseCouponViewController alloc] init];
    }
    return _unuseVC;
}
- (UseRecordViewController *)useRecordVC {
    if (!_useRecordVC) {
        _useRecordVC = [[UseRecordViewController alloc] init];
    }
    return _useRecordVC;
}
- (ExpiredViewController *)expiredVC {
    if (!_expiredVC) {
        _expiredVC = [[ExpiredViewController alloc] init];
    }
    return _expiredVC;
}
- (XXPageTabView *)pageTabView {
    if (!_pageTabView) {
        self.titleArr = @[@"未使用",@"使用记录",@"已过期"];
        _pageTabView = [[XXPageTabView alloc] initWithChildControllers:self.childViewControllers childTitles:self.titleArr];
        _pageTabView.delegate = self;
        _pageTabView.indicatorStyle = XXPageTabIndicatorStyleFollowText;//跟随文本i变化长度
        _pageTabView.titleStyle = XXPageTabTitleStyleGradient;//渐变
        _pageTabView.selectedTabIndex = 0;
        _pageTabView.separatorColor = [UIColor tipsGrayColor];
        _pageTabView.tabItemFont = [UIFont systemFontOfSize:14];
        _pageTabView.indicatorHeight = 2;
        _pageTabView.unSelectedColor = [UIColor colorWithHexString:@"#4A4A4A"];
        _pageTabView.selectedColor = [UIColor textBlackColor];
        _pageTabView.bodyBounces = YES;
    }
    return _pageTabView;
}


- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = [[NSArray alloc] init];
    }
    return _titleArr;
}
@end
